package cn.xawl.gulimall.search.web;

import cn.xawl.gulimall.search.service.SearchService;
import cn.xawl.gulimall.search.vo.SearchParams;
import cn.xawl.gulimall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lh
 * @create 2020-10-25 11:04
 **/
@Controller
public class SearchIndexController {


    @Autowired
    private SearchService searchService;

    @RequestMapping({"/","/search.html"})
    public String search(SearchParams params, Model model, HttpServletRequest request) {
        params.set_queryString(request.getQueryString());
        if (!StringUtils.isEmpty(params.get_queryString())) {

            params.set_url(request.getRequestURL().toString() + "?" + params.get_queryString());
        }
        SearchResult result = searchService.search(params);
        model.addAttribute("result", result);
        return "search";
    }

}
